package com.yxx.community.controller;

import com.yxx.community.dto.CommentDTO;
import com.yxx.community.dto.QuestionDTO;
import com.yxx.community.enums.CommentTypeEnum;
import com.yxx.community.service.CommentService;
import com.yxx.community.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @Author: YuXinXin
 * @Description:
 * @Date:Created in 16:49 2020/1/29
 **/
@Controller
public class QuestionController {

  @Autowired
  private QuestionService questionService;

  @Autowired
  private CommentService commentService;

  @GetMapping("/question/{id}")
  public String question(@PathVariable("id")long id,
                         Model model){
    QuestionDTO questionDTO=questionService.getById(id);
    List<QuestionDTO> relatedQuestions =questionService.selectRelated(questionDTO);
    List<CommentDTO> comments=commentService.listByTargetId(id, CommentTypeEnum.QUESTION);
    //累加阅读数
    questionService.inView(id);
    model.addAttribute("question",questionDTO);
    model.addAttribute("comments",comments);
    model.addAttribute("relatedQuestions",relatedQuestions);
    return "question";
  }
}
